import { ts, SwitchStatement, rule, BreakStatement, is, CallExpression, BinaryExpression } from "qvog-dsl";

const SEVERITY = 2;
const DOC_PATH = 'docs/consistent-type-imports-check.md';
const DESCRIPTION = 'Enforce consistent usage of type imports.';

enum TypeImportsPrefer {
    noType = 0,
    type = 1
}

let prefer = 1;

function containsAssert(nodeList: ts.Node[]): boolean {
    for (const node of nodeList) {
        if (ts.isAssertClause(node)) {
            return true;
        }
    }
    return false;
}

export default rule()
    .match((node): node is ts.ImportDeclaration => ts.isImportDeclaration(node))
    .when((node): boolean => {
        const nodeList = node.getChildren();
        if (nodeList.length < 3 || containsAssert(nodeList)) {
            return false;
        };
        const second = nodeList[1];
        if (!ts.isImportClause(second)) {
            return false;
        };
        const secondNodeList = second.getChildren();
        if (secondNodeList.length < 1) {
            return false;
        };
        const secondFirst = secondNodeList[0];
        const hasType = secondFirst.kind === ts.SyntaxKind.TypeKeyword;
        if ((prefer === TypeImportsPrefer.noType && hasType) ||
            (prefer === TypeImportsPrefer.type && !hasType)) {
            return true;
        };
        return false;
    })
    .report({
        severity: SEVERITY,
        description: DESCRIPTION,
        docPath: DOC_PATH,
    });
